package com.seu.githubcn.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seu.githubcn.config.Result;
import com.seu.githubcn.entity.Message;
import com.seu.githubcn.entity.User;
import com.seu.githubcn.entity.Repo;
import com.seu.githubcn.mapper.MessageMapper;
import com.seu.githubcn.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping()
public class MessageController {

    @Autowired
    UserMapper userMapper;

    @Autowired
    MessageMapper messageMapper;

    @GetMapping("/messages/unread")
    public Result<?> unread_messagePage(@RequestParam(defaultValue = "1") Integer currentPage,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                 @RequestParam(defaultValue = "0") String userIds){
        Integer userId= Integer.parseInt(userIds);
        Page<Message> messagePage =
                messageMapper.selectPage(new Page<>(currentPage,pageSize),
                        Wrappers.<Message>lambdaQuery()
                                .eq(Message::getUserId,userId).eq(Message::getStatus,1));
        return new Result<>(200,messagePage,"通知查询成功!");
    }

    @GetMapping("/messages")
    public Result<?> messagePage(@RequestParam(defaultValue = "1") Integer currentPage,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                 @RequestParam(defaultValue = "0") String userIds){
        Integer userId= Integer.parseInt(userIds);
        Page<Message> messagePage =
                messageMapper.selectPage(new Page<>(currentPage,pageSize),
                        Wrappers.<Message>lambdaQuery()
                                .eq(Message::getUserId,userId));
        return new Result<>(200,messagePage,"通知查询成功!");
    }

    @PostMapping("/message/read")
    public Result<?> readMessage(@RequestBody Message message){
        Message newOne = messageMapper.selectById(message.getMessageId());
        newOne.setStatus(false);
        messageMapper.updateById(newOne);
        return new Result<>(200,null,"修改成功!");
    }

    @PostMapping("/message/read/all")
    public Result<?> readAllMessage(@RequestBody User user){
        Integer userId;
        userId=user.getUserId();
        var messageList =
                messageMapper.selectList(Wrappers.<Message>lambdaQuery()
                        .eq(Message::getUserId,userId)
                        .eq(Message::getStatus,true));
        for (var one : messageList){
            one.setStatus(false);
            messageMapper.updateById(one);
        }
        return new Result<>(200,null,"修改成功!");
    }

    @PostMapping("/message/new")
    public Result<?> newMessage(@RequestBody Message message){
        Message newOne = new Message();
        newOne.setTitle(message.getTitle());
        newOne.setContent(message.getContent());
        newOne.setUserId(message.getUserId());
        newOne.setStatus(true);
        messageMapper.insert(newOne);
        return new Result<>(200,null,"新增成功!");
    }

}
